<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：新闻公告控制器。
 *
 **/

namespace Admin\Controller;

class NewsController extends ComController
{
    /**
     * 列表
     */
    public function index()
    {
        $type = I('get.type','');
        $key = I('get.keyword','','htmlspecialchars');
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('news');
        
        if($type){
            $where['type'] = $type;
        }
        
        if($key){
            $where['title'] = array('like','%'.$key.'%');
        }
        $pagesize =20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->order('t desc')->where($where)->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('type',$type);
        $this->assign('p',$p);
        $this->display();  
    }
    
    
    /**
     * 新增新闻
     */
    public function add()
    {
        if(IS_POST)
        {
            $data['title'] = I('post.title','','htmlspecialchars');
            $data['type'] = I('post.type',1,'htmlspecialchars');
            $data['px'] = I('post.px',99,'htmlspecialchars');
            $data['content'] = isset($_POST['content']) ? $_POST['content'] : false;
            if(!$data['title']){
                $this->error('标题不能为空！');
            }
            $data['t'] = time();
            $res = M('news')->data($data)->add();
            if($res){
                addlog('新增通知公告，ID：'.$res);
                //新增消息表
                M('user_message')->data(array('uid'=>0,'content'=>$data['title'],'type'=>1,'t'=>time(),'relation_id'=>$res,'url'=>'/newsdetails?id='.$res))->add();
                $this->success('操作成功',U('index'));
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }
        else
        {
           $this->assign('method',U('News/add'));
           $this->display('form');
        }
    }
    
    
    /**
     * 修改新闻
     */
    public function update()
    {
        $id = I('id');
        $p = I('p',1);
        if(!$id){
            $this->error('传参为空！');
        }
        if(IS_POST){
            $data['title'] = I('post.title','','htmlspecialchars');
            $data['type'] = I('post.type',1,'htmlspecialchars');
            $data['content'] = isset($_POST['content']) ? $_POST['content'] : false;
            $data['px'] = I('post.px',99,'htmlspecialchars');
            if(!$data['title']){
                $this->error('标题不能为空！');
            }
            $data['update_t'] = time();
            $res = M('news')->data($data)->where('id='.$id)->save();
            if($res){
                addlog('修改通知公告，ID：'.$id);
                //更新消息表
                M('user_message')->data(array('content'=>$data['title'],'update_t'=>time()))->where(array('relation_id'=>$id))->save();
                $this->success('操作成功',U('index',array('p'=>$p)));
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }else{
            $vo = M('news')->where('id='.$id)->find();
            if(!$vo){
                $this->error('记录不存在！');
            }
            $this->assign('method',U('News/update',array('p'=>$p)));
            $this->assign('vo',$vo);
            $this->assign('p',$p);
            $this->display('form');
        }
    }
    
    
    /**
     * 删除新闻
     */
    public function delete()
    {
        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        if ($ids) {
            if (is_array($ids)) {
                $ids = implode(',', $ids);
                $map['id'] = array('in', $ids);
                $where['relation_id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
                $where = 'relation_id=' . $ids;
            }
            if (M('news')->where($map)->delete()) {
                addlog('删除通知公告，ID：' . $ids);
                //删除消息表
                M('user_message')->where($where)->delete();
                $this->success('恭喜，删除通知公告成功！',U('index',array('p'=>$p)));
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }
}